﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AstekServis.Web.Data;
using System.ServiceModel.DomainServices.Client;

namespace AstekServis.Views.Admin
{
    public partial class ServisMerkeziTanitim : ChildWindow
    {
        AsTekDomainContext context = new AsTekDomainContext();
        public enum KartTipleri
        {
            ServisMerkezi,
            AracMarkalari,
            ServisSorunlari,
            TeknisyenTanitim,
            AracOzellikleri,
            SistemTanimlari,
            FirmwareTanimlari,
            KontrolTanimlari,
            FilmTanimlari,
            KapamaSebepleri,
            Parametreler,
            HizmetKartlari
        }
        public KartTipleri KartTipi { get; set; }

        public ServisMerkeziTanitim()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ServisMerkeziTanitim_Loaded);
            cmbFirma.SelectionChanged += cmbFirma_SelectionChanged;
        }

        void cmbFirma_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbFirma.SelectedIndex > -1)
            {
                LoadDepolar();
            }
        }

        void ServisMerkeziTanitim_Loaded(object sender, RoutedEventArgs e)
        {
            switch (this.KartTipi)
            {
                case KartTipleri.ServisMerkezi:
                    this.Title = "Servis Merkezi Tanıtım";
                    agDataGrid1.AddColumn("Adi");
                    lblDeger.Text = "Email :";
                    LoadServisMerkezleri();
                    break;
                case KartTipleri.AracMarkalari:
                    this.Title = "Araç Markalari Tanıtım";
                    agDataGrid1.AddColumn("MarkaAdi");
                    LoadAracMarkalari();
                    break;
                case KartTipleri.ServisSorunlari:
                    this.Title = "Servis Sorunları Tanıtım";
                    agDataGrid1.AddColumn("SorunTanimi");
                    lblAmir.Text = "Tip :";
                    lblAmir.Visibility = System.Windows.Visibility.Visible;
                    cmbAmir.Visibility = System.Windows.Visibility.Visible;
                    cmbAmir.Items.Add("Talep");
                    cmbAmir.Items.Add("Çözüm");
                    cmbAmir.SelectedIndex = 0;
                    LoadServisSorunTanimlari();
                    break;
                case KartTipleri.TeknisyenTanitim:
                    this.Title = "Teknisyen Tanıtım";
                    this.Height = 460;
                    this.Width = 650;
                    agDataGrid1.AddColumn("Adi");
                    lblAmir.Visibility = System.Windows.Visibility.Visible;
                    cmbAmir.Visibility = System.Windows.Visibility.Visible;
                    panelTeknisyen.Visibility = System.Windows.Visibility.Visible;
                    LoadTeknisyen();
                    break;
                case KartTipleri.AracOzellikleri:
                    this.Title = "Araç Özellikleri";
                    agDataGrid1.AddColumn("OzellikAdi");
                    lblAmir.Text = "Zorunlu :";
                    lblAmir.Visibility = System.Windows.Visibility.Visible;
                    cmbAmir.Visibility = System.Windows.Visibility.Visible;
                    cmbAmir.Items.Add("Evet");
                    cmbAmir.Items.Add("Hayır");
                    cmbAmir.SelectedIndex = 0;
                    LoadOzellik();
                    break;
                case KartTipleri.SistemTanimlari:
                    this.Title = "Sistem Tanımları";
                    agDataGrid1.AddColumn("Aciklama");
                    LoadSistem();
                    break;
                case KartTipleri.FirmwareTanimlari:
                    this.Title = "Firmware Tanımları";
                    agDataGrid1.AddColumn("Aciklama");
                    LoadFirmware();
                    break;
                case KartTipleri.KontrolTanimlari:
                    this.Title = "Kontrol Tanımları";
                    agDataGrid1.AddColumn("KontrolAdi");
                    LoadKontrolTanimlari();
                    break;
                case KartTipleri.FilmTanimlari:
                    this.Title = "Film Tanımları";
                    agDataGrid1.AddColumn("Adi");
                    LoadFilmTanimlari();
                    break;
                case KartTipleri.KapamaSebepleri:
                    this.Title = "Kapama Sebepleri";
                    agDataGrid1.AddColumn("Adi");
                    LoadKapamaSebepleri();
                    break;
                case KartTipleri.Parametreler:
                    this.Title = "Parametreler";
                    CancelButton.Visibility = System.Windows.Visibility.Collapsed;
                    agDataGrid1.AddColumn("Aciklama");
                    lblDeger.Visibility = System.Windows.Visibility.Visible;
                    txtDeger.Visibility = System.Windows.Visibility.Visible;
                    LoadParametreler();
                    break;
                case KartTipleri.HizmetKartlari:
                    this.Title = "Hizmet Kartı";
                    agDataGrid1.AddColumn("Aciklama");
                    lblDeger.Visibility = System.Windows.Visibility.Visible;
                    txtDeger.Visibility = System.Windows.Visibility.Visible;
                    lblHizKod.Visibility = System.Windows.Visibility.Visible;
                    txtHizkod.Visibility = System.Windows.Visibility.Visible;
                    LoadHizmetKartlari();
                    break;
                default:
                    break;
            }
        }

        private void LoadServisMerkezleri()
        {
            EntityQuery<ServisMerkezi> qry = context.GetServisMerkezleriQuery().OrderBy(x => x.Adi);
            LoadOperation<ServisMerkezi> operation = context.Load(qry);
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<ServisMerkezi> op = ss as LoadOperation<ServisMerkezi>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    agDataGrid1.DataSource = op.Entities;
                    newScreen();
                }
            };
        }
        private void LoadAracMarkalari()
        {
            EntityQuery<AracMarka> qry = context.GetAracMarkalariQuery().OrderBy(x => x.MarkaAdi);
            LoadOperation<AracMarka> operation = context.Load(qry);
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<AracMarka> op = ss as LoadOperation<AracMarka>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    agDataGrid1.DataSource = op.Entities;
                    newScreen();
                }
            };
        }
        private void LoadServisSorunTanimlari()
        {
            EntityQuery<ServisSorunTanimi> qry = context.GetServisSorunTanimlariQuery().OrderBy(x => x.SorunTanimi);
            LoadOperation<ServisSorunTanimi> operation = context.Load(qry);
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<ServisSorunTanimi> op = ss as LoadOperation<ServisSorunTanimi>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    agDataGrid1.DataSource = op.Entities;
                    newScreen();
                }
            };
        }
        private void LoadTeknisyen()
        {
            cmbFirma.Items.Add("Ayna Sanat");
            cmbFirma.Items.Add("Smart Media");

            EntityQuery<Teknisyen> qry = context.GetTeknisyenlerQuery().OrderBy(x => x.Adi);
            LoadOperation<Teknisyen> operation = context.Load(qry);
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<Teknisyen> op = ss as LoadOperation<Teknisyen>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    agDataGrid1.DataSource = op.Entities;
                    cmbAmir.ItemsSource = op.Entities;
                    cmbAmir.DisplayMemberPath = "Adi";
                    newScreen();
                }
            };
        }
        private void LoadOzellik()
        {
            EntityQuery<OzellikTanimi> qry = context.GetOzellikTanimlariQuery().OrderBy(x => x.OzellikAdi);
            LoadOperation<OzellikTanimi> operation = context.Load(qry);
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<OzellikTanimi> op = ss as LoadOperation<OzellikTanimi>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    agDataGrid1.DataSource = op.Entities;
                    newScreen();
                }
            };
        }
        private void LoadSistem()
        {
            EntityQuery<SistemTanim> qry = context.GetSistemTanimlariQuery().OrderBy(x => x.Aciklama);
            LoadOperation<SistemTanim> operation = context.Load(qry);
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<SistemTanim> op = ss as LoadOperation<SistemTanim>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    agDataGrid1.DataSource = op.Entities;
                    newScreen();
                }
            };
        }
        private void LoadFirmware()
        {
            EntityQuery<FirmwareTanim> qry = context.GetFirmwareTanimlariQuery().OrderBy(x => x.Aciklama);
            LoadOperation<FirmwareTanim> operation = context.Load(qry);
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<FirmwareTanim> op = ss as LoadOperation<FirmwareTanim>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    agDataGrid1.DataSource = op.Entities;
                    newScreen();
                }
            };
        }
        private void LoadKontrolTanimlari()
        {
            EntityQuery<ServisKontrolTanimi> qry = context.GetServisKontrolTanimlariQuery().OrderBy(x => x.KontrolAdi);
            LoadOperation<ServisKontrolTanimi> operation = context.Load(qry);
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<ServisKontrolTanimi> op = ss as LoadOperation<ServisKontrolTanimi>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    agDataGrid1.DataSource = op.Entities;
                    newScreen();
                }
            };
        }
        private void LoadFilmTanimlari()
        {
            EntityQuery<Film> qry = context.GetFilmlerQuery().OrderBy(x => x.Adi);
            LoadOperation<Film> operation = context.Load(qry);
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<Film> op = ss as LoadOperation<Film>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    agDataGrid1.DataSource = op.Entities;
                    newScreen();
                }
            };
        }
        private void LoadDepolar()
        {
            if (cmbFirma.SelectedIndex > -1)
            {
                EntityQuery<Depo> qry = context.GetDepolarQuery().Where(x => x.dbNo == cmbFirma.SelectedIndex).OrderBy(x => x.Adi);
                LoadOperation<Depo> operation = context.Load(qry);
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<Depo> op = ss as LoadOperation<Depo>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        LstDepolar.SelectionMode = SelectionMode.Multiple;
                        cmbDepolar.DisplayMemberPath = "Adi";
                        cmbArizaDepo.DisplayMemberPath = "Adi";
                        LstDepolar.DisplayMemberPath = "Adi";

                        cmbDepolar.Items.Clear();
                        cmbArizaDepo.Items.Clear();
                        LstDepolar.Items.Clear();

                        foreach (var item in op.Entities)
                        {
                            cmbDepolar.Items.Add(item);
                            cmbArizaDepo.Items.Add(item);
                            LstDepolar.Items.Add(item);
                        }
                    }
                };
            }
        }
        private void LoadKapamaSebepleri()
        {
            EntityQuery<KapamaSebebi> qry = context.GetKapamaSebepleriQuery().OrderBy(x => x.Adi);
            LoadOperation<KapamaSebebi> operation = context.Load(qry);
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<KapamaSebebi> op = ss as LoadOperation<KapamaSebebi>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    agDataGrid1.DataSource = op.Entities;
                    newScreen();
                }
            };
        }
        private void LoadParametreler()
        {
            EntityQuery<Parametre> qry = context.GetParametrelerQuery().OrderBy(x => x.Aciklama);
            LoadOperation<Parametre> operation = context.Load(qry);
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<Parametre> op = ss as LoadOperation<Parametre>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    agDataGrid1.DataSource = op.Entities;
                }
            };
        }
        private void LoadHizmetKartlari()
        {
            EntityQuery<HizmetKarti> qry = context.GetHizmetKartlariQuery().OrderBy(x => x.Aciklama);
            LoadOperation<HizmetKarti> operation = context.Load(qry);
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<HizmetKarti> op = ss as LoadOperation<HizmetKarti>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    agDataGrid1.DataSource = op.Entities;
                }
            };
        }

        private void SaveServisMerkezleri()
        {
            if (!string.IsNullOrEmpty(txtAdi.Text))
            {
                int id = Cevir.Parse<int>(txtAdi.Tag);
                LoadOperation<ServisMerkezi> operation = context.Load(context.GetServisMerkezleriQuery().Where(x => x.ID == id));
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<ServisMerkezi> op = ss as LoadOperation<ServisMerkezi>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        ServisMerkezi srv = op.Entities.FirstOrDefault();
                        if (srv != null)
                        {
                            srv.Email = txtDeger.Text;
                            srv.Adi = txtAdi.Text;
                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                            }, null);
                        }
                        else
                        {
                            ServisMerkezi newsrv = new ServisMerkezi();
                            newsrv.ID = -1;
                            newsrv.Adi = txtAdi.Text;
                            newsrv.Email = txtDeger.Text;
                            context.ServisMerkezis.Add(newsrv);
                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                                else
                                {
                                    LoadServisMerkezleri();
                                }
                            }, null);
                        }
                    }
                };
            }
        }
        private void SaveServisSorunTanimlari()
        {
            if (!string.IsNullOrEmpty(txtAdi.Text))
            {
                int id = Cevir.Parse<int>(txtAdi.Tag);
                LoadOperation<ServisSorunTanimi> operation = context.Load(context.GetServisSorunTanimlariQuery().Where(x => x.ID == id));
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<ServisSorunTanimi> op = ss as LoadOperation<ServisSorunTanimi>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        ServisSorunTanimi srv = op.Entities.FirstOrDefault();
                        if (srv != null)
                        {
                            srv.SorunTanimi = txtAdi.Text;
                            srv.Tip = (byte)cmbAmir.SelectedIndex;
                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                            }, null);
                        }
                        else
                        {
                            ServisSorunTanimi newsrv = new ServisSorunTanimi();
                            newsrv.SorunTanimi = txtAdi.Text;
                            newsrv.Tip = (byte)cmbAmir.SelectedIndex;
                            context.ServisSorunTanimis.Add(newsrv);
                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                                else
                                {
                                    LoadServisSorunTanimlari();
                                }
                            }, null);
                        }
                    }
                };
            }
        }
        private void SaveAracMarkalari()
        {
            if (!string.IsNullOrEmpty(txtAdi.Text))
            {
                int id = Cevir.Parse<int>(txtAdi.Tag);
                LoadOperation<AracMarka> operation = context.Load(context.GetAracMarkalariQuery().Where(x => x.ID == id));
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<AracMarka> op = ss as LoadOperation<AracMarka>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        AracMarka srv = op.Entities.FirstOrDefault();
                        if (srv != null)
                        {
                            srv.MarkaAdi = txtAdi.Text;
                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                            }, null);
                        }
                        else
                        {
                            AracMarka newsrv = new AracMarka();
                            newsrv.MarkaAdi = txtAdi.Text;

                            context.AracMarkas.Add(newsrv);
                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                                else
                                {
                                    LoadAracMarkalari();
                                }
                            }, null);
                        }
                    }
                };
            }
        }
        private void SaveTeknisyen()
        {
            if (!string.IsNullOrEmpty(txtAdi.Text))
            {
                int id = Cevir.Parse<int>(txtAdi.Tag);
                LoadOperation<Teknisyen> operation = context.Load(context.GetTeknisyenlerQuery().Where(x => x.ID == id));
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<Teknisyen> op = ss as LoadOperation<Teknisyen>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        Teknisyen srv = op.Entities.FirstOrDefault();
                        if (srv != null)
                        {
                            srv.Adi = txtAdi.Text;
                            Teknisyen amr = (Teknisyen)cmbAmir.SelectedItem;
                            if (amr != null)
                                srv.AmirID = amr.ID;
                            else
                                srv.AmirID = 0;

                            if (cmbDepolar.SelectedIndex > -1)
                            {
                                srv.DepoNo = ((Depo)cmbDepolar.SelectedItem).No.Value;
                            }

                            if (cmbArizaDepo.SelectedIndex > -1)
                            {
                                srv.ArizaliDepoNo = ((Depo)cmbArizaDepo.SelectedItem).No.Value;
                            }

                            string kulldepo = "";
                            foreach (var d in LstDepolar.SelectedItems)
                            {
                                kulldepo += ((Depo)d).No.Value.ToString() + ";";
                            }

                            if (!string.IsNullOrEmpty(kulldepo))
                                srv.KullanilabilirDepolar = kulldepo.Remove(kulldepo.Length - 1);
                            else
                                srv.KullanilabilirDepolar = "-1";

                            srv.EvrakSeri = txtSeri.Text;
                            srv.MikroKullanici = txtMikKullanici.Text;
                            srv.PlasiyerKodu = txtPlasiyer.Text;
                            srv.Sifre = txtSifre.Text;
                            srv.SrmMerkezi = txtSrmMerkezKodu.Text;
                            srv.SubeNo = txtSubeNo.Text;
                            srv.FirmaNo = txtFirmaNo.Text;
                            srv.DbNo = cmbFirma.SelectedIndex;

                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                            }, null);
                        }
                        else
                        {
                            Teknisyen newsrv = new Teknisyen();
                            newsrv.Adi = txtAdi.Text;
                            Teknisyen amr = (Teknisyen)cmbAmir.SelectedItem;
                            if (amr != null)
                                newsrv.AmirID = amr.ID;
                            else
                                newsrv.AmirID = 0;

                            if (cmbDepolar.SelectedIndex > -1)
                            {
                                newsrv.DepoNo = ((Depo)cmbDepolar.SelectedItem).No.Value;
                            }

                            if (cmbArizaDepo.SelectedIndex > -1)
                            {
                                newsrv.ArizaliDepoNo = ((Depo)cmbArizaDepo.SelectedItem).No.Value;
                            }

                            string kulldepo = "";
                            foreach (var d in LstDepolar.SelectedItems)
                            {
                                kulldepo += ((Depo)d).No.Value.ToString() + ";";
                            }

                            if (!string.IsNullOrEmpty(kulldepo))
                                newsrv.KullanilabilirDepolar = kulldepo.Remove(kulldepo.Length - 1);
                            else
                                newsrv.KullanilabilirDepolar = "-1";

                            newsrv.MikroKullanici = txtMikKullanici.Text;
                            newsrv.PlasiyerKodu = txtPlasiyer.Text;
                            newsrv.Sifre = txtSifre.Text;
                            newsrv.SrmMerkezi = txtSrmMerkezKodu.Text;
                            newsrv.SubeNo = txtSubeNo.Text;
                            newsrv.FirmaNo = txtFirmaNo.Text;
                            newsrv.EvrakSeri = txtSeri.Text;
                            newsrv.DbNo = cmbFirma.SelectedIndex;

                            context.Teknisyens.Add(newsrv);
                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                                else
                                {
                                    LoadTeknisyen();
                                }
                            }, null);
                        }
                    }
                };
            }
        }
        private void SaveOzellik()
        {
            if (!string.IsNullOrEmpty(txtAdi.Text))
            {
                int id = Cevir.Parse<int>(txtAdi.Tag);
                LoadOperation<OzellikTanimi> operation = context.Load(context.GetOzellikTanimlariQuery().Where(x => x.ID == id));
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<OzellikTanimi> op = ss as LoadOperation<OzellikTanimi>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        OzellikTanimi oz = op.Entities.FirstOrDefault();
                        if (oz != null)
                        {
                            oz.OzellikAdi = txtAdi.Text;
                            if (cmbAmir.SelectedIndex == 0)
                                oz.Zorunlu = true;
                            else
                                oz.Zorunlu = false;

                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                            }, null);
                        }
                        else
                        {
                            OzellikTanimi newoz = new OzellikTanimi();
                            newoz.OzellikAdi = txtAdi.Text;
                            if (cmbAmir.SelectedIndex == 0)
                                newoz.Zorunlu = true;
                            else
                                newoz.Zorunlu = false;

                            context.OzellikTanimis.Add(newoz);
                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                                else
                                {
                                    LoadOzellik();
                                }
                            }, null);
                        }
                    }
                };
            }
        }
        private void SaveSistem()
        {
            if (!string.IsNullOrEmpty(txtAdi.Text))
            {
                int id = Cevir.Parse<int>(txtAdi.Tag);
                LoadOperation<SistemTanim> operation = context.Load(context.GetSistemTanimlariQuery().Where(x => x.ID == id));
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<SistemTanim> op = ss as LoadOperation<SistemTanim>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        SistemTanim oz = op.Entities.FirstOrDefault();
                        if (oz != null)
                        {
                            oz.Aciklama = txtAdi.Text;
                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                            }, null);
                        }
                        else
                        {
                            SistemTanim newoz = new SistemTanim();
                            newoz.Aciklama = txtAdi.Text;

                            context.SistemTanims.Add(newoz);
                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                                else
                                {
                                    LoadSistem();
                                }
                            }, null);
                        }
                    }
                };
            }
        }
        private void SaveFirmware()
        {
            if (!string.IsNullOrEmpty(txtAdi.Text))
            {
                int id = Cevir.Parse<int>(txtAdi.Tag);
                LoadOperation<FirmwareTanim> operation = context.Load(context.GetFirmwareTanimlariQuery().Where(x => x.ID == id));
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<FirmwareTanim> op = ss as LoadOperation<FirmwareTanim>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        FirmwareTanim oz = op.Entities.FirstOrDefault();
                        if (oz != null)
                        {
                            oz.Aciklama = txtAdi.Text;
                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                            }, null);
                        }
                        else
                        {
                            FirmwareTanim newoz = new FirmwareTanim();
                            newoz.Aciklama = txtAdi.Text;

                            context.FirmwareTanims.Add(newoz);
                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                                else
                                {
                                    LoadFirmware();
                                }
                            }, null);
                        }
                    }
                };
            }
        }
        private void SaveKontrolTanimlari()
        {
            if (!string.IsNullOrEmpty(txtAdi.Text))
            {
                int id = Cevir.Parse<int>(txtAdi.Tag);
                LoadOperation<ServisKontrolTanimi> operation = context.Load(context.GetServisKontrolTanimlariQuery().Where(x => x.ID == id));
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<ServisKontrolTanimi> op = ss as LoadOperation<ServisKontrolTanimi>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        ServisKontrolTanimi oz = op.Entities.FirstOrDefault();
                        if (oz != null)
                        {
                            oz.KontrolAdi = txtAdi.Text;
                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                            }, null);
                        }
                        else
                        {
                            ServisKontrolTanimi newoz = new ServisKontrolTanimi();
                            newoz.KontrolAdi = txtAdi.Text;

                            context.ServisKontrolTanimis.Add(newoz);
                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                                else
                                {
                                    LoadKontrolTanimlari();
                                }
                            }, null);
                        }
                    }
                };
            }
        }
        private void SaveFilmTanimlari()
        {
            if (!string.IsNullOrEmpty(txtAdi.Text))
            {
                int id = Cevir.Parse<int>(txtAdi.Tag);
                LoadOperation<Film> operation = context.Load(context.GetFilmlerQuery().Where(x => x.ID == id));
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<Film> op = ss as LoadOperation<Film>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        Film oz = op.Entities.FirstOrDefault();
                        if (oz != null)
                        {
                            oz.Adi = txtAdi.Text;
                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                            }, null);
                        }
                        else
                        {
                            Film newoz = new Film();
                            newoz.Adi = txtAdi.Text;

                            context.Films.Add(newoz);
                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                                else
                                {
                                    LoadFilmTanimlari();
                                }
                            }, null);
                        }
                    }
                };
            }
        }
        private void SaveKapamaSebepleri()
        {
            if (!string.IsNullOrEmpty(txtAdi.Text))
            {
                int id = Cevir.Parse<int>(txtAdi.Tag);
                LoadOperation<KapamaSebebi> operation = context.Load(context.GetKapamaSebepleriQuery().Where(x => x.ID == id));
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<KapamaSebebi> op = ss as LoadOperation<KapamaSebebi>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        KapamaSebebi oz = op.Entities.FirstOrDefault();
                        if (oz != null)
                        {
                            oz.Adi = txtAdi.Text;
                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                            }, null);
                        }
                        else
                        {
                            KapamaSebebi newoz = new KapamaSebebi();
                            newoz.Adi = txtAdi.Text;

                            context.KapamaSebebis.Add(newoz);
                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                                else
                                {
                                    LoadKapamaSebepleri();
                                }
                            }, null);
                        }
                    }
                };
            }
        }
        private void SaveParametre()
        {
            if (!string.IsNullOrEmpty(txtAdi.Text) && !string.IsNullOrEmpty(txtDeger.Text))
            {
                LoadOperation<Parametre> operation = context.Load(context.GetParametrelerQuery().Where(x => x.Kodu == Cevir.Parse<int>(txtAdi.Tag)));
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<Parametre> op = ss as LoadOperation<Parametre>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        Parametre srv = op.Entities.FirstOrDefault();
                        if (srv != null)
                        {
                            srv.Kodu = Cevir.Parse<int>(txtAdi.Tag);
                            srv.Deger = Cevir.Parse<int>(txtDeger.Text);
                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                            }, null);
                        }
                        else
                        {
                            Parametre newsrv = new Parametre();
                            newsrv.Kodu = Cevir.Parse<int>(txtAdi.Tag);
                            newsrv.Deger = Cevir.Parse<int>(txtDeger.Text);

                            context.Parametres.Add(newsrv);
                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                                else
                                {
                                    LoadParametreler();
                                }
                            }, null);
                        }
                    }
                };
            }
        }
        private void SaveHizmetKartlari()
        {
            if (!string.IsNullOrEmpty(txtAdi.Text) && !string.IsNullOrEmpty(txtDeger.Text) && !string.IsNullOrEmpty(txtHizkod.Text))
            {
                LoadOperation<HizmetKarti> operation = context.Load(context.GetHizmetKartlariQuery().Where(x => x.ID == Cevir.Parse<int>(txtAdi.Tag)));
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<HizmetKarti> op = ss as LoadOperation<HizmetKarti>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        HizmetKarti srv = op.Entities.FirstOrDefault();
                        if (srv != null)
                        {
                            srv.Aciklama = txtAdi.Text;
                            srv.Fiyati = Cevir.Parse<double>(txtDeger.Text);
                            srv.HizmetKodu = txtHizkod.Text;

                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                                else
                                    newScreen();
                            }, null);
                        }
                        else
                        {
                            HizmetKarti newsrv = new HizmetKarti();
                            newsrv.Aciklama = txtAdi.Text;
                            newsrv.Fiyati = Cevir.Parse<double>(txtDeger.Text);
                            newsrv.HizmetKodu = txtHizkod.Text;

                            context.HizmetKartis.Add(newsrv);
                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                                else
                                {
                                    newScreen();
                                    LoadHizmetKartlari();
                                }
                            }, null);
                        }
                    }
                };
            }
        }


        private void DeleteServisMerkezleri()
        {
            if (!string.IsNullOrEmpty(txtAdi.Text))
            {
                LoadOperation<ServisMerkezi> operation = context.Load(context.GetServisMerkezleriQuery().Where(x => x.Adi == txtAdi.Text));
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<ServisMerkezi> op = ss as LoadOperation<ServisMerkezi>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        ServisMerkezi usr = op.Entities.FirstOrDefault();
                        context.ServisMerkezis.Remove(usr);
                        context.SubmitChanges(so =>
                        {
                            if (so.HasError)
                                MessageBox.Show("Hata:\r\n" + so.Error.Message);
                            else
                            {
                                LoadServisMerkezleri();
                            }
                        }, null);
                    }
                };
            }
        }
        private void DeleteServisSorunTanimlari()
        {
            if (!string.IsNullOrEmpty(txtAdi.Text))
            {
                LoadOperation<ServisSorunTanimi> operation = context.Load(context.GetServisSorunTanimlariQuery().Where(x => x.SorunTanimi == txtAdi.Text));
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<ServisSorunTanimi> op = ss as LoadOperation<ServisSorunTanimi>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        ServisSorunTanimi usr = op.Entities.FirstOrDefault();
                        context.ServisSorunTanimis.Remove(usr);
                        context.SubmitChanges(so =>
                        {
                            if (so.HasError)
                                MessageBox.Show("Hata:\r\n" + so.Error.Message);
                            else
                            {
                                LoadServisSorunTanimlari();
                            }
                        }, null);
                    }
                };
            }
        }
        private void DeleteAracMarkalari()
        {
            if (!string.IsNullOrEmpty(txtAdi.Text))
            {
                LoadOperation<AracMarka> operation = context.Load(context.GetAracMarkalariQuery().Where(x => x.MarkaAdi == txtAdi.Text));
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<AracMarka> op = ss as LoadOperation<AracMarka>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        AracMarka usr = op.Entities.FirstOrDefault();
                        context.AracMarkas.Remove(usr);
                        context.SubmitChanges(so =>
                        {
                            if (so.HasError)
                                MessageBox.Show("Hata:\r\n" + so.Error.Message);
                            else
                            {
                                LoadAracMarkalari();
                            }
                        }, null);
                    }
                };
            }
        }
        private void DeleteTeknisyen()
        {
            if (!string.IsNullOrEmpty(txtAdi.Text))
            {
                LoadOperation<Teknisyen> operation = context.Load(context.GetTeknisyenlerQuery().Where(x => x.Adi == txtAdi.Text));
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<Teknisyen> op = ss as LoadOperation<Teknisyen>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        Teknisyen usr = op.Entities.FirstOrDefault();
                        usr.Adi = txtAdi.Text;
                        Teknisyen amr = (Teknisyen)cmbAmir.SelectedItem;
                        if (amr != null)
                            usr.AmirID = amr.ID;
                        else
                            usr.AmirID = 0;

                        context.Teknisyens.Remove(usr);
                        context.SubmitChanges(so =>
                        {
                            if (so.HasError)
                                MessageBox.Show("Hata:\r\n" + so.Error.Message);
                            else
                            {
                                LoadTeknisyen();
                            }
                        }, null);
                    }
                };
            }
        }
        private void DeleteOzellik()
        {
            if (!string.IsNullOrEmpty(txtAdi.Text))
            {
                LoadOperation<OzellikTanimi> operation = context.Load(context.GetOzellikTanimlariQuery().Where(x => x.OzellikAdi == txtAdi.Text));
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<OzellikTanimi> op = ss as LoadOperation<OzellikTanimi>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        OzellikTanimi oz = op.Entities.FirstOrDefault();
                        oz.OzellikAdi = txtAdi.Text;

                        if (cmbAmir.SelectedIndex == 0)
                            oz.Zorunlu = true;
                        else
                            oz.Zorunlu = false;

                        context.OzellikTanimis.Remove(oz);
                        context.SubmitChanges(so =>
                        {
                            if (so.HasError)
                                MessageBox.Show("Hata:\r\n" + so.Error.Message);
                            else
                            {
                                LoadOzellik();
                            }
                        }, null);
                    }
                };
            }
        }
        private void DeleteSistemTanim()
        {
            if (!string.IsNullOrEmpty(txtAdi.Text))
            {
                LoadOperation<SistemTanim> operation = context.Load(context.GetSistemTanimlariQuery().Where(x => x.Aciklama == txtAdi.Text));
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<SistemTanim> op = ss as LoadOperation<SistemTanim>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        SistemTanim oz = op.Entities.FirstOrDefault();
                        oz.Aciklama = txtAdi.Text;

                        context.SistemTanims.Remove(oz);
                        context.SubmitChanges(so =>
                        {
                            if (so.HasError)
                                MessageBox.Show("Hata:\r\n" + so.Error.Message);
                            else
                            {
                                LoadSistem();
                            }
                        }, null);
                    }
                };
            }
        }
        private void DeleteFirmware()
        {
            if (!string.IsNullOrEmpty(txtAdi.Text))
            {
                LoadOperation<FirmwareTanim> operation = context.Load(context.GetFirmwareTanimlariQuery().Where(x => x.Aciklama == txtAdi.Text));
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<FirmwareTanim> op = ss as LoadOperation<FirmwareTanim>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        FirmwareTanim oz = op.Entities.FirstOrDefault();
                        oz.Aciklama = txtAdi.Text;

                        context.FirmwareTanims.Remove(oz);
                        context.SubmitChanges(so =>
                        {
                            if (so.HasError)
                                MessageBox.Show("Hata:\r\n" + so.Error.Message);
                            else
                            {
                                LoadFirmware();
                            }
                        }, null);
                    }
                };
            }
        }
        private void DeleteKontrolTanim()
        {
            if (!string.IsNullOrEmpty(txtAdi.Text))
            {
                LoadOperation<ServisKontrolTanimi> operation = context.Load(context.GetServisKontrolTanimlariQuery().Where(x => x.KontrolAdi == txtAdi.Text));
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<ServisKontrolTanimi> op = ss as LoadOperation<ServisKontrolTanimi>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        ServisKontrolTanimi oz = op.Entities.FirstOrDefault();
                        oz.KontrolAdi = txtAdi.Text;

                        context.ServisKontrolTanimis.Remove(oz);
                        context.SubmitChanges(so =>
                        {
                            if (so.HasError)
                                MessageBox.Show("Hata:\r\n" + so.Error.Message);
                            else
                            {
                                LoadKontrolTanimlari();
                            }
                        }, null);
                    }
                };
            }
        }
        private void DeleteFilm()
        {
            if (!string.IsNullOrEmpty(txtAdi.Text))
            {
                LoadOperation<Film> operation = context.Load(context.GetFilmlerQuery().Where(x => x.Adi == txtAdi.Text));
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<Film> op = ss as LoadOperation<Film>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        Film oz = op.Entities.FirstOrDefault();
                        oz.Adi = txtAdi.Text;

                        context.Films.Remove(oz);
                        context.SubmitChanges(so =>
                        {
                            if (so.HasError)
                                MessageBox.Show("Hata:\r\n" + so.Error.Message);
                            else
                            {
                                LoadFilmTanimlari();
                            }
                        }, null);
                    }
                };
            }
        }
        private void DeleteKapamaSebebi()
        {
            if (!string.IsNullOrEmpty(txtAdi.Text))
            {
                LoadOperation<KapamaSebebi> operation = context.Load(context.GetKapamaSebepleriQuery().Where(x => x.Adi == txtAdi.Text));
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<KapamaSebebi> op = ss as LoadOperation<KapamaSebebi>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        KapamaSebebi oz = op.Entities.FirstOrDefault();
                        oz.Adi = txtAdi.Text;

                        context.KapamaSebebis.Remove(oz);
                        context.SubmitChanges(so =>
                        {
                            if (so.HasError)
                                MessageBox.Show("Hata:\r\n" + so.Error.Message);
                            else
                            {
                                LoadKapamaSebepleri();
                            }
                        }, null);
                    }
                };
            }
        }
        private void DeleteHizmetKarti()
        {
            if (txtAdi.Tag != null)
            {
                LoadOperation<HizmetKarti> operation = context.Load(context.GetHizmetKartlariQuery().Where(x => x.ID == Convert.ToInt32(txtAdi.Tag)));
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<HizmetKarti> op = ss as LoadOperation<HizmetKarti>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        HizmetKarti oz = op.Entities.FirstOrDefault();

                        context.HizmetKartis.Remove(oz);
                        context.SubmitChanges(so =>
                        {
                            if (so.HasError)
                                MessageBox.Show("Hata:\r\n" + so.Error.Message);
                            else
                            {
                                newScreen();
                                LoadHizmetKartlari();
                            }
                        }, null);
                    }
                };
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            switch (this.KartTipi)
            {
                case KartTipleri.ServisMerkezi:
                    SaveServisMerkezleri();
                    break;
                case KartTipleri.AracMarkalari:
                    SaveAracMarkalari();
                    break;
                case KartTipleri.ServisSorunlari:
                    SaveServisSorunTanimlari();
                    break;
                case KartTipleri.TeknisyenTanitim:
                    SaveTeknisyen();
                    break;
                case KartTipleri.AracOzellikleri:
                    SaveOzellik();
                    break;
                case KartTipleri.SistemTanimlari:
                    SaveSistem();
                    break;
                case KartTipleri.FirmwareTanimlari:
                    SaveFirmware();
                    break;
                case KartTipleri.KontrolTanimlari:
                    SaveKontrolTanimlari();
                    break;
                case KartTipleri.FilmTanimlari:
                    SaveFilmTanimlari();
                    break;
                case KartTipleri.KapamaSebepleri:
                    SaveKapamaSebepleri();
                    break;
                case KartTipleri.Parametreler:
                    SaveParametre();
                    break;
                case KartTipleri.HizmetKartlari:
                    SaveHizmetKartlari();
                    break;
                default:
                    break;
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            switch (this.KartTipi)
            {
                case KartTipleri.ServisMerkezi:
                    DeleteServisMerkezleri();
                    break;
                case KartTipleri.AracMarkalari:
                    DeleteAracMarkalari();
                    break;
                case KartTipleri.ServisSorunlari:
                    DeleteServisSorunTanimlari();
                    break;
                case KartTipleri.TeknisyenTanitim:
                    DeleteTeknisyen();
                    break;
                case KartTipleri.AracOzellikleri:
                    DeleteOzellik();
                    break;
                case KartTipleri.SistemTanimlari:
                    DeleteSistemTanim();
                    break;
                case KartTipleri.FirmwareTanimlari:
                    DeleteFirmware();
                    break;
                case KartTipleri.KontrolTanimlari:
                    DeleteKontrolTanim();
                    break;
                case KartTipleri.FilmTanimlari:
                    DeleteFilm();
                    break;
                case KartTipleri.KapamaSebepleri:
                    DeleteKapamaSebebi();
                    break;
                case KartTipleri.HizmetKartlari:
                    DeleteHizmetKarti();
                    break;
                default:
                    break;
            }
        }

        private void newButton_Click(object sender, RoutedEventArgs e)
        {
            newScreen();
        }

        private void newScreen()
        {
            txtAdi.Text = "";
            if (cmbAmir.Visibility == System.Windows.Visibility.Visible && cmbAmir.Items.Count > 0)
                cmbAmir.SelectedIndex = 0;

            txtFirmaNo.Text = "";
            txtMikKullanici.Text = "";
            txtPlasiyer.Text = "";
            txtSifre.Text = "";
            txtSrmMerkezKodu.Text = "";
            txtSubeNo.Text = "";
            cmbDepolar.SelectedIndex = -1;
            txtDeger.Text = "";
            txtHizkod.Text = "";
            txtAdi.Tag = -1;
        }

        private void agDataGrid1_RowClick(object sender, DevExpress.AgDataGrid.RowClickEventArgs e)
        {
            switch (this.KartTipi)
            {
                case KartTipleri.ServisMerkezi:
                    ServisMerkezi srv = (ServisMerkezi)agDataGrid1.FocusedDataRow;
                    txtAdi.Tag = srv.ID;
                    txtAdi.Text = srv.Adi;
                    txtDeger.Text = srv.Email;
                    break;
                case KartTipleri.AracMarkalari:
                    AracMarka mrk = (AracMarka)agDataGrid1.FocusedDataRow;
                    txtAdi.Tag = mrk.ID;
                    txtAdi.Text = mrk.MarkaAdi;
                    break;
                case KartTipleri.ServisSorunlari:
                    ServisSorunTanimi srn = (ServisSorunTanimi)agDataGrid1.FocusedDataRow;
                    txtAdi.Tag = srn.ID;
                    txtAdi.Text = srn.SorunTanimi;
                    cmbAmir.SelectedIndex = srn.Tip;
                    break;
                case KartTipleri.TeknisyenTanitim:
                    Teknisyen tk = (Teknisyen)agDataGrid1.FocusedDataRow;
                    txtAdi.Tag = tk.ID;
                    txtAdi.Text = tk.Adi;
                    foreach (Teknisyen item in cmbAmir.Items)
                    {
                        if (tk.AmirID == item.ID)
                            cmbAmir.SelectedItem = item;
                    }
                    txtFirmaNo.Text = tk.FirmaNo;
                    txtMikKullanici.Text = tk.MikroKullanici;
                    txtPlasiyer.Text = tk.PlasiyerKodu;
                    txtSifre.Text = tk.Sifre;
                    txtSrmMerkezKodu.Text = tk.SrmMerkezi;
                    txtSubeNo.Text = tk.SubeNo;
                    txtSeri.Text = tk.EvrakSeri;
                    cmbFirma.SelectedIndex = tk.DbNo;

                    foreach (Depo item in cmbDepolar.Items)
                    {
                        if (tk.DepoNo == item.No)
                            cmbDepolar.SelectedItem = item;
                    }

                    foreach (Depo item in cmbArizaDepo.Items)
                    {
                        if (tk.ArizaliDepoNo == item.No)
                            cmbArizaDepo.SelectedItem = item;
                    }

                    break;
                case KartTipleri.AracOzellikleri:
                    OzellikTanimi oz = (OzellikTanimi)agDataGrid1.FocusedDataRow;
                    txtAdi.Tag = oz.ID;
                    txtAdi.Text = oz.OzellikAdi;
                    if (oz.Zorunlu == true)
                        cmbAmir.SelectedIndex = 0;
                    else
                        cmbAmir.SelectedIndex = 1;
                    break;
                case KartTipleri.SistemTanimlari:
                    SistemTanim tnm = (SistemTanim)agDataGrid1.FocusedDataRow;
                    txtAdi.Tag = tnm.ID;
                    txtAdi.Text = tnm.Aciklama;
                    break;
                case KartTipleri.FirmwareTanimlari:
                    FirmwareTanim frm = (FirmwareTanim)agDataGrid1.FocusedDataRow;
                    txtAdi.Tag = frm.ID;
                    txtAdi.Text = frm.Aciklama;
                    break;
                case KartTipleri.KontrolTanimlari:
                    ServisKontrolTanimi ktr = (ServisKontrolTanimi)agDataGrid1.FocusedDataRow;
                    txtAdi.Tag = ktr.ID;
                    txtAdi.Text = ktr.KontrolAdi;
                    break;
                case KartTipleri.FilmTanimlari:
                    Film flm = (Film)agDataGrid1.FocusedDataRow;
                    txtAdi.Tag = flm.ID;
                    txtAdi.Text = flm.Adi;
                    break;
                case KartTipleri.KapamaSebepleri:
                    KapamaSebebi sbp = (KapamaSebebi)agDataGrid1.FocusedDataRow;
                    txtAdi.Tag = sbp.ID;
                    txtAdi.Text = sbp.Adi;
                    break;
                case KartTipleri.Parametreler:
                    Parametre prm = (Parametre)agDataGrid1.FocusedDataRow;
                    txtAdi.Text = prm.Aciklama;
                    txtAdi.Tag = prm.Kodu;
                    txtDeger.Text = prm.Deger.ToString();
                    break;
                case KartTipleri.HizmetKartlari:
                    HizmetKarti hiz = (HizmetKarti)agDataGrid1.FocusedDataRow;
                    txtAdi.Text = hiz.Aciklama;
                    txtAdi.Tag = hiz.ID;
                    txtHizkod.Text = hiz.HizmetKodu;
                    txtDeger.Text = hiz.Fiyati.ToString();
                    break;
                default:
                    break;
            }
        }
    }
}

